<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Campaign_Functions
 *
 * @author Samuel Pulta
 */
class Campaign_Functions extends Functions {

    public function energy_chart_quarter($db,$quarter){
        $name = $quarter . "_" . date("Y");
        $query = $db->query("SELECT * FROM " . $db->tbl_name(TB_ENERGY_CHART) . " WHERE name={$db->escape($name)}
        AND is_active = '1'","row");
        
        return $query;
    }


    public function campaign_month($type){
        $camp = $this->campaign_month_val($type);
        $ret = '';
            foreach($camp as $val){
                $ret .= '<th>';
                    $ret .= $val;
                $ret .= '</th>';
            }
            
        
        return $ret;
    }
    
    public function ec_brand_story($data){
        $brand_story = array("0"    =>  "2015 Environment Holidays",
                            "1"     =>  "2015 Sales Event",
                            "2"     =>  "Brochure Enhancement",
                            "3"     =>  "Other Support",
                            "4"     =>  "# of Pages",
                            "5"     =>  "Tupperware Brands Platform CY13",
                            "6"     =>  "Platform Lead Products");
        
        if(isset($data) && !empty($data)){
            $array = array();
            $split_id = explode(",",$data['td_arrangement']);
            foreach($split_id as $id_key){
                $array[$id_key] = $brand_story[$id_key];
            }
            return $array;
            //var_dump($array);
        }else{
            return $brand_story;
        }
        
            
    }

    /*
        @tr_length = "";
        @tr_length_val = "6";
        @td_length_val = "8";
    */
    public function brand_story_table($query,$tr_length_eq,$tr_length_val,$td_length_val_first,$td_length_val,$td_length_val_eq,$quarter,$data,$array = array()){
        $db = $array['db'];
        $ret = "";
        $id_split = "";
        $brand_story = $this->ec_brand_story($data);
        //$data = $query;
        //for($tr_length = $tr_length_eq; $tr_length < $tr_length_val; $tr_length++) {
        foreach($brand_story as $tr_length=>$tr_val){
            
            $ret .= '<tr class="td_arr_sorting" data-type="' . $quarter . '" data-td-arr="' . $tr_length . '" id="drag_energy_chart_' . $tr_length . '" style="width:100%;">';
                    for($td_length = $td_length_val_eq; $td_length < $td_length_val; $td_length++){
                    $story_list = "";
                    if($td_length == $td_length_val_first){
                        $story_list = "sort";
                    }
                    

                        $ret .= '<td data-sort-val="' . $tr_length . '" data-brand-story-list="' . $story_list . '" style="height: 100px;width:100px;">';
                            $ret .= '<ul>';
                            if($td_length == $td_length_val_first){
                                $ret .= "<b>" . $tr_val . "</b>";
                            }else{
                                
                                // Load Campaign Version Only
                                if(isset($array['data_load']) && $array['data_load'] == true){
                                    
                                    $data = $query;
                                   
                                    $encrypted_id = $this->base_encode_decode('encrypt',$data['campaign_id']);
                                    
                                        $data_arr = array("data"    =>  $data,
                                                          "td_length"    =>  $td_length,
                                                          "tr_length"    =>  $tr_length,
                                                          "quarter"    =>  $quarter,
                                                          "encrypted_id"    =>  $encrypted_id,
                                                          "array"    =>  $array);
                                        
                                        $ret .= $this->td_cell($data_arr);
                                   
                                    // Load Campaign Only
                                    if(isset($array['query'])){
                                        foreach($array['query'] as $data){
                                            $encrypted_id = $this->base_encode_decode('encrypt',$data['campaign_id']);
                                        
                                            $data_arr = array("data"    =>  $data,
                                                          "td_length"    =>  $td_length,
                                                          "tr_length"    =>  $tr_length,
                                                          "quarter"    =>  $quarter,
                                                          "encrypted_id"    =>  $encrypted_id,
                                                          "array"    =>  $array);
                                            
                                            $ret .= $this->td_cell($data_arr);
                                        }
                                    }
                                }else{
                                    // Load Default Only
                                    foreach($query as $data){
                                    
                                    $encrypted_id = $this->base_encode_decode('encrypt',$data['campaign_id']);
                                    
                                        $sql = "SELECT * FROM " . $db->tbl_name(TB_CAMPAIGN_VERSION) . "
                                        WHERE campaign_id = {$db->escape($data['campaign_id'])} ORDER BY version_id DESC";
                                        
                                        $campaing_query  = $db->query($sql,"row");
                                        $campaing_query_nums  = $db->query($sql,"numrows");
                                        if($campaing_query_nums != 0){
                                            $data = $campaing_query;
                                        }else{
                                            $data = $data;
                                        }
                                        
                                        $data_arr = array("data"    =>  $data,
                                                          "td_length"    =>  $td_length,
                                                          "tr_length"    =>  $tr_length,
                                                          "quarter"    =>  $quarter,
                                                          "encrypted_id"    =>  $encrypted_id,
                                                          "array"    =>  $array);
                                        
                                        $ret .= $this->td_cell($data_arr);
                                    }
                                }
                                
                            }
                                
                            $ret .= '</ul>';
                        $ret .= '</td>';
                    }
                
            $ret .= '</tr>';
            
        }
        return $ret;
    }
    
    public function td_cell($data_arr){
        $ret = "";
        $arr = array("campaign_title"       =>  $data_arr['data']['title'],
                    "campaign_id"           =>  $data_arr['data']['campaign_list_id'],
                    "campaign_description"  =>  $data_arr['data']['description'],
                    "brand_story"           =>  $data_arr['data']['brand_story']
                    );
    
        $td = $data_arr['td_length'] - 1;
            if($td == $data_arr['data']['campaign_list_id'] && $data_arr['data']['brand_story'] == $data_arr['tr_length']){
                $ret .= '<li>';
                    if($data_arr['array']['editable'] == true){
                        $ret .= '<a  class="energy_chart cursor ec_val_' . $data_arr['quarter'] . '" data-id="' . $data_arr['encrypted_id'] . '" data-container="body" data-toggle="popover" data-placement="left" data-title="' . $data_arr['data']['title'] . '" style="color:blue;text-decoration:underline;">';
                            $ret .= $this->truncateStringWords($data_arr['data']['description'], "30");
                        $ret .= '</a>';
                        $ret .= '';
                        $ret .= '<div id="ec_td_val_' . $data_arr['encrypted_id'] . '" style="display: none">';
                            $ret .= '<div data-json="' . htmlentities(json_encode($arr)) . '">';
                                $ret .= $data_arr['data']['description'];
                                $ret .= '<br>';
                                $ret .= '<div class="pull-right"  data-id="' . $data_arr['encrypted_id'] . '">';
                                    $ret .= '<button data-type="edit" class="trigger_campaign btn btn-small" data-id="tbl_ec_' . $data_arr['quarter'] . '" href="#ec_modal_update" role="button"  data-toggle="modal" type="button">Edit</button>';
                                    //$ret .= ' <button class="trigger_campaign btn btn-small" type="button">Save</button>';
                                $ret .= '</div>';
                            $ret .= '</div>';
                        $ret .= '</div>';
                    }else{
                        $ret .= $this->truncateStringWords($data_arr['data']['description'], "30");
                    }
                $ret .= '</li>';
            }
            
            return $ret;
    }
    
    public function array_attributes($db,$arr = array()){
        if($arr['quarter'] == null){
            $class = "tb_brochure";
            if($arr['allow'] == true){
                $array = array("editable"   =>  true, "db" => $db, "data_load"  => true, "query"    => $arr['query']);
            }else{
                $array = array("editable"   =>  true, "db" => $db);
            }
            
            $action = "";
            $allow_drag = "drag_energy_chart";
            $status = "";
        }else{
            if($arr['quarter']['energy_chart_status'] == "1"){
                $class = "tb_brochure";
                if($arr['allow'] == true){
                    $array = array("editable"   =>  true, "db" => $db, "data_load"  => true, "query"    => $arr['query']);
                }else{
                    $array = array("editable"   =>  true, "db" => $db);
                }
                $action = "";
                $allow_drag = "drag_energy_chart";
                $status = "Completed";
            }else{
                $class = "tb_brochure_final";
                if($arr['allow'] == true){
                $array = array("editable"   =>  false, "db" => $db, "data_load"  => true, "query"    => $arr['query']);
                }else{
                    $array = array("editable"   =>  false, "db" => $db);
                }
                $action = "hide";
                $allow_drag = "";
                $status = "Final";
            }
        }
        
        $return = array("class" =>  $class,
                        "array" =>  $array,
                        "action"=>  $action,
                        "allow_drag"    =>  $allow_drag,
                        "status"        =>  $status);
        
        return $return;
    }
    
    public function brand_story($name,$style,$class,$id,$attr){
        $brand_story = array(   "0"    =>  "2015 Environment Holidays",
                                "1"    =>  "2015 Sales Event",
                                "2"     =>  "Brochure Enhancement",
                                "3"     =>  "Other Support",
                                "4"     =>  "# of Pages",
                                "5"     =>  "Tupperware Brands Platform CY13",
                                "6"     =>  "Platform Lead Products");
        
        $ret = "";
            $ret .= '<select name="' . $name . '" ' . $style . ' ' . $attr . ' class="' . $class . '" id="' . $id . '" >';
                $ret .= '<option value="">--- Select ---</option>';
                for($a = 0; $a < 7; $a++){
                    $ret .= '<option value="' . $a . '">' . $brand_story[$a] . '</option>';
                }
            $ret .= '</select>';
        return $ret;
    }
    
        
    
    public function select_campaign($name,$style,$class,$id,$attr){
    
        $ret = "";
            $ret .= '<select name="' . $name . '" ' . $style . ' ' . $attr . ' class="' . $class . '" id="' . $id . '" >';
                $ret .= '<option value="">--- Select ---</option>';
                $ret .= '<option value="1">JAN 1</option>';
                $ret .= '<option value="2">JAN 2</option>';
                $ret .= '<option value="3">FEB 1</option>';
                $ret .= '<option value="4">FEB 2</option>';
                $ret .= '<option value="5">MAR 1</option>';
                $ret .= '<option value="6">MAR 2</option>';
                $ret .= '<option value="7">APR 1</option>';
                $ret .= '<option value="8">APR 2</option>';
                $ret .= '<option value="9">MAY 1</option>';
                $ret .= '<option value="10">MAY 2</option>';
                $ret .= '<option value="11">JUN 1</option>';
                $ret .= '<option value="12">JUN 2</option>';
                $ret .= '<option value="13">JUL 1</option>';
                $ret .= '<option value="14">JUL 2</option>';
                $ret .= '<option value="15">AUG 1</option>';
                $ret .= '<option value="16">AUG 2</option>';
                $ret .= '<option value="17">SEP 1</option>';
                $ret .= '<option value="18">SEP 2</option>';
                $ret .= '<option value="19">OCT 1</option>';
                $ret .= '<option value="20">OCT 2</option>';
                $ret .= '<option value="21">NOV 1</option>';
                $ret .= '<option value="22">NOV 2</option>';
                $ret .= '<option value="23">DEC 1</option>';
                $ret .= '<option value="24">DEC 2</option>';
            $ret .= '</select>';
    
            return $ret;
    }
    
    public function reload_selected_campaign($val){
    
        $ret = "";
            switch($val){
                case "1":
                    return "JAN C1";
                    break; 
                case "2":
                    return "JAN C2";
                    break;   
                case "3":
                    return "FEB C1";
                    break;   
                case "4":
                    return "FEB C2";
                    break;   
                case "5":
                    return "MAR C1";
                    break;   
                case "6":
                    return "MAR C2";
                    break;   
                case "7":
                    return "APR C1";
                    break;   
                case "8":
                    return "APR C2";
                    break;   
                case "9":
                    return "MAY C1";
                    break;   
                case "10":
                    return "MAY C2";
                    break;   
                case "11":
                    return "JUN C1";
                    break;   
                case "12":
                    return "JUN C2";
                    break;   
                case "13":
                    return "JUL C1";
                    break;   
                case "14":
                    return "AUG C1";
                    break;   
                case "15":
                    return "AUG C2";
                    break;   
                case "16":
                    return "SEP C1";
                    break;   
                case "17":
                    return "SEP C1";
                    break;
                case "18":
                    return "SEP C2";
                    break;
                case "19":
                    return "OCT C1";
                    break;
                case "20":
                    return "OCT C2";
                    break;
                case "21":
                    return "NOV C1";
                    break;
                case "22":
                    return "NOV C2";
                    break;
                case "23":
                    return "DEC C1";
                    break;
                case "24":
                    return "DEC C2";
                    break;
                    
            }
    }
    
    public function campaign_month_val($type){
    
        switch($type){
            case "Quarter 1":
                $arr = array("1"    =>  "JAN C1",
                    "2"    =>  "JAN C2",
                    "3"    =>  "FEB C1",
                    "4"    =>  "FEB C2",
                    "5"    =>  "MAR C1",
                    "6"    =>  "MAR C2"
                    
        );
                break;
            case "Quarter 2":
                $arr = array("7"    =>  "APR C1",
                    "8"    =>  "APR C2",
                    "9"    =>  "MAY C1",
                    "10"    =>  "MAY C2",
                    "11"    =>  "JUN C1",
                    "12"    =>  "JUN C2"
        );
                break;
            case "Quarter 3":
                $arr = array("13"    =>  "JUL C1",
                    "14"    =>  "JUL C2",
                    "15"    =>  "AUG C1",
                    "16"    =>  "AUG C2",
                    "17"    =>  "SEP C1",
                    "18"    =>  "SEP C2"
        );
                break;
            case "Quarter 4":
                $arr = array("19"    =>  "OCT C1",
                    "20"    =>  "OCT C2",
                    "21"    =>  "NOV C1",
                    "22"    =>  "NOV C2",
                    "23"    =>  "DEC C1",
                    "24"    =>  "DEC C2"
        );
                break;
            case "Full":
                $arr = array("1"    =>  "JAN C1",
                    "2"    =>  "JAN C2",
                    "3"    =>  "FEB C1",
                    "4"    =>  "FEB C2",
                    "5"    =>  "MAR C1",
                    "6"    =>  "MAR C2",
                    "7"    =>  "APR C1",
                    "8"    =>  "APR C2",
                    "9"    =>  "MAY C1",
                    "10"    =>  "MAY C2",
                    "11"    =>  "JUN C1",
                    "12"    =>  "JUN C2",
                    "13"    =>  "JUL C1",
                    "14"    =>  "JUL C2",
                    "15"    =>  "AUG C1",
                    "16"    =>  "AUG C2",
                    "17"    =>  "SEP C1",
                    "18"    =>  "SEP C2",
                    "19"    =>  "OCT C1",
                    "20"    =>  "OCT C2",
                    "21"    =>  "NOV C1",
                    "22"    =>  "NOV C2",
                    "23"    =>  "DEC C1",
                    "24"    =>  "DEC C2"
        );
                break;
        }
        
        
        return $arr;
        
    }
    
    
}

?>
